using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for TopTenProcessesWebPart
/// </summary>
namespace SqlWebAdmin.CSharp.Controls
{
    public class TopTenProcessesWebPart : CurrentServerWebPart
    {
        public TopTenProcessesWebPart()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected override void RenderWebPart(Microsoft.SqlServer.Management.Smo.Server server, HtmlTextWriter writer)
        {
            if (server != null)
            {
                DataSet ds = server.Databases["master"].ExecuteWithResults("select top 10 spid, cpu, loginame from master..sysprocesses order by cpu desc");
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt != null)
                    {
                        int count = 0;
                        writer.WriteLine("<table border=\"0\" >");
                        foreach (DataRow row in dt.Rows)
                        {
                            writer.WriteLine("<tr><td>{0}.</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", count + 1, row[0].ToString(), row[1].ToString(), row[2].ToString());
                            if (count++ > 10)
                                break;
                        }
                        writer.WriteLine("</table>");
                    }
                }
            }
        }
}
}